package youleche.com.common;

import java.util.Calendar;
import java.util.List;

import youleche.com.business.BArea;
import youleche.com.factory.MLLogFactory;
import youleche.com.model.MArea;

/**
 * 补抓中国行政区域数据
 * @author Administrator
 *
 */
public class RepairChainaAreaCreawler {
	/**
	 * 统计局官方url
	 */
	private String statsUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/";
	
	/**
	 * 多少年的行政区域数据
	 */
	private String year = "";
	
	/**
	 * 私有构造函数
	 * @param year 年份
	 */
	private RepairChainaAreaCreawler(String year){
		this.year = year;
	}
	
	/**
	 * 构造函数私有化
	 */
	private RepairChainaAreaCreawler(){
		
	}
	
	/**
	 * 补抓爬虫
	 */
	private static RepairChainaAreaCreawler crawler = null;
	
	/**
	 * 获取爬虫实例
	 * @param year  年份
	 */
	public static RepairChainaAreaCreawler getInstance(String year){
		if(crawler == null){
			crawler = new RepairChainaAreaCreawler(year);
		}
		
		return crawler;
	} 
	
	/**
	 * 获取爬虫实例（默认为当前年）
	 */
	public static RepairChainaAreaCreawler getInstance(){
		if(crawler == null){
			Calendar cal = Calendar.getInstance();
		    int year = cal.get(Calendar.YEAR);
			crawler = new RepairChainaAreaCreawler(""+year+"");
		}
		
		return crawler;
	} 
	
	/**
	 * 获取最新的数据
	 * @return 返回最新的区域数据
	 */
	public MArea getLastArea(){
		BArea handler = new BArea();
		MArea newArea = null;
		try {
			String areaCode = handler.getOnlyOneAreaByAddTimeDesc();
			newArea = handler.getAreaByAreaCode(areaCode);
		} catch (Exception e) {
			e.printStackTrace();
			MLLogFactory.exceptionLog("获取最新的区域数据代码失败", "获取最新的区域数据代码失败", e.getMessage(), "127.0.0.1");
		}
		
		return newArea;
	}
	
	/**
	 * 根据区域数据代码获取url后缀
	 * @param code 代码
	 * @param level 区域等级
	 * @return 返回url后缀
	 */
	public String getSuffixUrlByLevelAndCode(String code, int level){
		String sufUrl = "";
		String url2 ="";
		String url3 ="";
		String url4 ="";
		String url5 ="";
		switch(level){
		case 1:
			sufUrl = "index.html";
			break;
		case 2:
			url2 = code.substring(0, 2);
			sufUrl = url2 + ".html";
			break;
		case 3:
			url2 = code.substring(0, 2);
			url3 = code.substring(2, 4);
			sufUrl = url2 + "/" + url2 + url3  + ".html";
			break;
		case 4:
			url2 = code.substring(0, 2);
			url3 = code.substring(2, 4);
			url4 = code.substring(4, 6);
			sufUrl = url2 + "/" + url3 + "/" + url2 + url3 + url4 + ".html";
			break;
		case 5:
			url2 = code.substring(0, 2);
			url3 = code.substring(2, 4);
			url4 = code.substring(4, 6);
			url5 = code.substring(6, 9);
			sufUrl = url2 + "/" + url3 + "/" +  url4 +"/" + url2 + url3 + url4 + url5 + ".html";
			break;
		}
		
		return sufUrl;
	}
	
	public static void main(String[] args) {
		RepairChainaAreaCreawler crawler = RepairChainaAreaCreawler.getInstance("2013");
		MArea  area = crawler.getLastArea();
		try {
			crawler.parseAreaByAreaCodeAndLevel(area);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String parseAreaByAreaCodeAndLevel(MArea area) throws Exception{
		String sufUrl = "";
		String url = "";
		int level = Integer.parseInt(area.getAreaLevel());
		MeilecheChinaAreaCrawler crawler = MeilecheChinaAreaCrawler.getInstance();
		MArea parea = new MArea();
		System.out.println("areaCode:" + area.getAreaCode());
		System.out.println("areaLevel:" + area.getAreaLevel());
		switch(level){
			case 5:
				sufUrl = this.getSuffixUrlByLevelAndCode(area.getAreaCode(), level);
				url = statsUrl + this.year + "/" + sufUrl; 
				crawler.parseCountryAndCode(url, area.getParentCode());
				parea.setAreaCode(area.getParentCode());
				parea.setAreaLevel("" + (level -1) + "");
				this.parseAreaByAreaCodeAndLevel(parea);
				break;
			case 4:
				sufUrl = this.getSuffixUrlByLevelAndCode(area.getAreaCode(), level);
				url = statsUrl + this.year + "/" + sufUrl; 
				crawler.parseCityAndCode(url, ""+level+"", area.getParentCode());
				parea.setAreaCode(area.getParentCode());
				parea.setAreaLevel("" + (level -1) + "");
				this.parseAreaByAreaCodeAndLevel(parea);
				break;
			case 3:
				sufUrl = this.getSuffixUrlByLevelAndCode(area.getAreaCode(), level);
				url = statsUrl + this.year + "/" + sufUrl; 
				crawler.parseCityAndCode(url, ""+level+"", area.getParentCode());
				parea.setAreaCode(area.getParentCode());
				parea.setAreaLevel("" + (level -1) + "");
				this.parseAreaByAreaCodeAndLevel(parea);
				break;
			case 2:
				sufUrl = this.getSuffixUrlByLevelAndCode(area.getAreaCode(), level);
				url = statsUrl + this.year + "/" + sufUrl; 
				crawler.parseCityAndCode(url, ""+level+"", area.getParentCode());
				parea.setAreaCode(area.getParentCode());
				parea.setAreaLevel("" + (level -1) + "");
				this.parseAreaByAreaCodeAndLevel(parea);
				break;
			case 1:
				sufUrl = this.getSuffixUrlByLevelAndCode(area.getAreaCode(), level);
				url = statsUrl + this.year + "/" + sufUrl; 
				crawler.parseProvinceAndCode(url);
				break;
				
		}
		
		return "";
	}
	
	
	
}
